import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xiongan_app_yy/entrys/rp/car_info_list_entity.dart';
import 'package:xiongan_app_yy/utils/license_plate_utils.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/badge.dart';
import 'package:xiongan_app_yy/widgets/comm/input_card_widget_new.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

import 'car_number_page_new.dart';

class CarInfoListSelect extends StatefulWidget {
  final Map<String, List<CarInfoListItem>> args;

  CarInfoListSelect(this.args);

  @override
  _CarInfoListSelectState createState() => _CarInfoListSelectState();
}

class _CarInfoListSelectState extends State<CarInfoListSelect> {
  List<CarInfoListItem> datas = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.args['datas'] != null && widget.args['datas'].length > 0) {
      datas.addAll(widget.args['datas']);
    } else {
      datas.add(CarInfoListItem());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        centerTitle: true,
        title: Text("车辆信息", style: TextStyle(fontSize: 18)),
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: ListView.builder(
              shrinkWrap: true,
              physics: ScrollPhysics(),
              itemBuilder: (context, index) {
                return CarItemInfo(datas.elementAt(index), index, changeItem);
              },
              scrollDirection: Axis.vertical,
              itemCount: datas.length,
              padding: EdgeInsets.only(top: 0),
            ),
          ),
          Container(
            width: double.infinity,
            height: 48,
            child: FlatButton(
              color: Color(0XFF00A5F1),
              child: Text(
                "确定",
                style: TextStyle(color: Colors.white),
              ),
              onPressed: () {
                checkData();
              },
            ),
          ),
        ],
      ),
    );
  }

  void changeItem(index) {
    setState(() {
      if (index == 0) {
        datas.add(CarInfoListItem());
      } else {
        datas.removeAt(index);
      }
    });
  }

  void checkData() {
    for (int i = 0; i < datas.length; i++) {
      if (datas[i].driver != null && datas[i].driver.length > 0) {
        if (datas[i].phone != null && datas[i].phone.length > 0) {
          if (datas[i].carno != null &&
              datas[i].carno.length > 0 &&
              LicensePlateUtils.isLicensePlate(datas[i].carno)) {
            if (i + 1 == datas.length) {
              Navigator.pop(context, datas);
            }
          } else {
            TostUtil.showToast("请填写第${i + 1}位正确的车牌号");
            break;
          }
        } else {
          TostUtil.showToast("请填写第${i + 1}位司机手机号");
          break;
        }
      } else {
        TostUtil.showToast("请填写第${i + 1}位司机姓名");
        break;
      }
    }
  }
}

class CarItemInfo extends StatefulWidget {
  CarInfoListItem item;
  int index;
  Function changeItem;

  CarItemInfo(this.item, this.index, this.changeItem);

  @override
  _CarItemInfoState createState() => _CarItemInfoState();
}

class _CarItemInfoState extends State<CarItemInfo> {
  TextEditingController controller0 = TextEditingController();
  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();
  String proText = "浙";

  @override
  void initState() {

  }

  @override
  Widget build(BuildContext context) {
    if (widget.item.driver != null && widget.item.driver.length > 0) {
      controller0.text = widget.item.driver;
    }
    if (widget.item.phone != null && widget.item.phone.length > 0) {
      controller1.text = widget.item.phone;
    }
    if (widget.item.carno != null && widget.item.carno.length > 0) {
      proText = widget.item.carno.substring(0, 1);
      controller2.text =
          widget.item.carno.substring(1, widget.item.carno.length);
    }
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 10, right: 10),
      child: Row(
        children: <Widget>[
          Center(
            child: Container(
              width: 24,
              height: 24,
              child: Badge(
                  padding: 6,
                  child: Text(""),
                  backgroundColor: Colors.blue,
                  text: "${widget.index + 1}"),
            ),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                InputCardWidgetNew(
                  hintText: "请输入司机姓名",
                  title: "司机姓名:",
                  contro: controller0,
                  onChange: () {
                    widget.item.driver = controller0.text;
                  },
                ),
                InputCardWidgetNew(
                  keyboardType: TextInputType.number,
                  hintText: "请输入司机手机号",
                  title: "司机手机号:",
                  onChange: () {
                    widget.item.phone = controller1.text;
                  },
                  contro: controller1,
                  inputFormatters: [
                    WhitelistingTextInputFormatter(RegExp("[0-9]")),
                    LengthLimitingTextInputFormatter(11),
                  ],
                ),
                CarNumberPageNew(true, controller2, proText,
                    (proText) => _changeText(proText), (value) {
                  widget.item.carno = proText + value;
                }),
              ],
            ),
          ),
          Center(
            child: RaisedButton(
                onPressed: () {
                  widget.changeItem(widget.index);
                },
                color: widget.index == 0 ? Colors.green : Colors.red,
                child: widget.index == 0
                    ? Text(
                        "+ 添加",
                        style: TextStyle(color: Colors.white, fontSize: 14),
                      )
                    : Text("- 删除",
                        style: TextStyle(color: Colors.white, fontSize: 14))),
          )
        ],
      ),
    );
  }

  _changeText(text) {
    proText = text;
  }
}
